package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DatInfo;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.Parts;
import com.ruoyi.system.mapper.DatInfoMapper;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.PartsMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备Service业务层处理
 *
 * @author wxd
 * @date 2024-08-25
 */
@Service
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private PartsMapper partsMapper;
    @Autowired
    private SysDeptMapper deptMapper;
    @Autowired
    private DatInfoMapper datInfoMapper;

    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceById(String id) {
        return deviceMapper.selectDeviceById(id);
    }

    /**
     * 查询设备列表
     *
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device) {
        List<Device> list = deviceMapper.selectDeviceList(device);
        if (CollUtil.isEmpty(list)) {
            return list;
        }
        List<Long> deptIds = list.stream().map(Device::getDeptId).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(deptIds)) {
            List<SysDept> depts = deptMapper.listByIds(deptIds.toArray(new Long[0]));
            Map<Long, SysDept> map = depts.stream().collect(Collectors.toMap(SysDept::getDeptId, p -> p));
            list.forEach(x -> {
                SysDept sysDept = map.get(x.getDeptId());
                if (sysDept != null) {
                    x.setDeptStr(sysDept.getDeptName() + "(" + sysDept.getAddress() + ")");
                    x.setDeptName(sysDept.getDeptName());
                }
            });
        }
        List<String> deviceIds = list.stream().map(Device::getId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(deviceIds)) {
            List<DatInfo> datInfos = datInfoMapper.listByDeviceIds(deviceIds.toArray(new String[0]));
            Map<String, DatInfo> map = datInfos.stream().collect(Collectors.toMap(DatInfo::getDeviceId, p -> p));
            list.forEach(x -> {
                DatInfo datInfo = map.get(x.getId());
                if (datInfo != null) {
                    x.setLastDatAllCount(datInfo.getAllCount());
                }
            });
        }
        List<String> deIds = new ArrayList<>();
        deIds.addAll(list.stream().map(Device::getHtId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getHnqId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getTfqId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getJzId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getJz2Id).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getDmId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getDxkId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getFxcId).collect(Collectors.toList()));
        deIds.addAll(list.stream().map(Device::getFsqId).collect(Collectors.toList()));
        if (CollectionUtil.isNotEmpty(deIds)) {
            List<Parts> devices = partsMapper.selectPartsByIds(deIds.toArray(new String[0]));
            if (CollectionUtil.isNotEmpty(devices)) {
                Map<String, Parts> map = devices.stream().collect(Collectors.toMap(Parts::getId, p -> p));
                list.forEach(x -> {
                    if (StrUtil.isNotEmpty(x.getHtId())) {
                        Parts parts = map.get(x.getHtId());
                        if (parts != null) {
                            x.setHtModel(parts.getModel());
                            x.setHtHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getHnqId())) {
                        Parts parts = map.get(x.getHnqId());
                        if (parts != null) {
                            x.setHnqModel(parts.getModel());
                            x.setHnqHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getTfqId())) {
                        Parts parts = map.get(x.getTfqId());
                        if (parts != null) {
                            x.setTfqModel(parts.getModel());
                            x.setTfqHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getJzId())) {
                        Parts parts = map.get(x.getJzId());
                        if (parts != null) {
                            x.setJzModel(parts.getModel());
                            x.setJzHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getJz2Id())) {
                        Parts parts = map.get(x.getJz2Id());
                        if (parts != null) {
                            x.setJz2Model(parts.getModel());
                            x.setJz2HjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getDmId())) {
                        Parts parts = map.get(x.getDmId());
                        if (parts != null) {
                            x.setDmModel(parts.getModel());
                            x.setDmHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getDxkId())) {
                        Parts parts = map.get(x.getDxkId());
                        if (parts != null) {
                            x.setDxkModel(parts.getModel());
                            x.setDxkHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getFxcId())) {
                        Parts parts = map.get(x.getFxcId());
                        if (parts != null) {
                            x.setFxcModel(parts.getModel());
                            x.setFxcHjTimes(parts.getHjTimes());
                        }
                    }
                    if (StrUtil.isNotEmpty(x.getFsqId())) {
                        Parts parts = map.get(x.getFsqId());
                        if (parts != null) {
                            x.setFsqModel(parts.getModel());
                            x.setFsqHjTimes(parts.getHjTimes());
                        }
                    }
                });
            }
        }
        return list;
    }

    /**
     * 新增设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device) {
        device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device) {
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(String[] ids) {
        return deviceMapper.deleteDeviceByIds(ids);
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(String id) {
        return deviceMapper.deleteDeviceById(id);
    }

    @Override
    public List<String> selectFieldGroup(String fieldName) {
        return deviceMapper.selectFieldGroup(fieldName);
    }
}
